package cn.shop.dys.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;

import java.text.DecimalFormat;
import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.activity.LogisticsActivity;
import cn.shop.dys.bean.AllIndentItem;
import cn.shop.dys.bean.CartItem;
import cn.shop.dys.bean.MyCheckoutAddup;
import cn.shop.dys.bean.OrderInfo;
import cn.shop.dys.bean.ProductInfo;
import cn.shop.dys.bean.ProductProperty;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.dys.util.BusFactory;
import cn.shop.dys.widget.SingleIndentItem;
import cn.shop.lib_common.CommonUtil;
import cn.shop.lib_common.CommonViewHolder;

/**
 * 订单适配器
 * Created by sunjian on 2016/4/20.
 */
public class WaitReceiveGoodsAdapter extends IndentBaseAdapter{
    private List<AllIndentItem> mAllIndentItems;
    private Context mContext;
    private int mType;
    private HttpUtils mHttpUtils;
    private View.OnClickListener mListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AllIndentItem allIndentItem= (AllIndentItem) v.getTag();
            /*cancelOrderInfo(allIndentItem,mHttpUtils,mContext);
            mAllIndentItems.remove(allIndentItem);
            notifyDataSetChanged();*/
            BusFactory.getBus().post(allIndentItem);
        }
    };
    private View.OnClickListener mStatusListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //   CommonUtil.showToast(mContext,"跳物流");
            Intent intent=new Intent(mContext, LogisticsActivity.class);
            mContext.startActivity(intent);
        }
    };
    private final BitmapUtils mBitmapUtils;

    public WaitReceiveGoodsAdapter(List<AllIndentItem> allIndentItems, Context mContext, int mType) {
        this.mAllIndentItems = allIndentItems;
        this.mContext = mContext;
        this.mType=mType;
        mHttpUtils=new HttpUtils();
        mBitmapUtils = new BitmapUtils(mContext);
    }

    @Override
    public int getCount() {
        return mAllIndentItems==null?0:mAllIndentItems.size();
    }

    @Override
    public AllIndentItem getItem(int position) {
        return mAllIndentItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommonViewHolder holder = CommonViewHolder.getCommonViewHolder(convertView, mContext, R.layout.indent_item_common_list);
        AllIndentItem allIndentItem = mAllIndentItems.get(position);
        disposeGetView(holder, allIndentItem);
        return holder.convertView;
    }
    private void disposeGetView(CommonViewHolder holder, AllIndentItem allIndentItem) {
        OrderInfo orderInfo = allIndentItem.getmOrderInfo();
        List<CartItem> cartItems = allIndentItem.getmCartItems();
        MyCheckoutAddup myCheckoutAddup = allIndentItem.getMyCheckoutAddup();
        // TODO: 2016/4/21 订单其他状态处理，数据库不匹配
        TextView tvIndentNumber = holder.getTv(R.id.tv_indent_number);
        tvIndentNumber.setText("订单编号:"+orderInfo.getTime());
        TextView tvIndentGoodsStatus = holder.getTv(R.id.tv_indent_goods_status);
        if(mType== CommonConstant.INDENT_TYPE_ALL){
            if(orderInfo.getFlag()==1){
                tvIndentGoodsStatus.setText("待付款");
            }else if(orderInfo.getFlag()==2){
                tvIndentGoodsStatus.setText("待收货");
            }else if(orderInfo.getFlag()==3){
                tvIndentGoodsStatus.setText("待评价");
            }
        }else {
            tvIndentGoodsStatus.setVisibility(View.GONE);
        }
        TextView tvIndentAllCount = holder.getTv(R.id.tv_indent_all_count);
        tvIndentAllCount.setText("共"+myCheckoutAddup.getTotalCount()+"件商品  实付:");
        TextView tvIndentAllPrice = holder.getTv(R.id.tv_indent_all_price);
        tvIndentAllPrice.setText("￥"+new DecimalFormat("#.00").format(myCheckoutAddup.getTotalPrice()));
        ImageView ivProductDelete = holder.getIv(R.id.iv_product_delete);
        ivProductDelete.setTag(allIndentItem);
        ivProductDelete.setOnClickListener(mListener);
        // TODO: 2016/4/21 图片 品牌名 规格 数量
        LinearLayout view = holder.getView(R.id.ll_child_add, LinearLayout.class);
        view.removeAllViews();
        for (CartItem cartItem : cartItems) {
            ProductInfo productInfo = cartItem.getProductInfo();
            SingleIndentItem item = new SingleIndentItem(mContext);
            item.setIndentImage(productInfo.getPic());
            item.setIndentName(productInfo.getName());
            item.setIndentPrice((int)productInfo.getPrice()+"");
            item.setIndentCount(cartItem.getProdNum()+"");
            List<ProductProperty> productProperty = productInfo.getProductProperty();
            StringBuilder sb=new StringBuilder();
            if(productProperty!=null||productProperty.size()==0){
                for (ProductProperty property : productProperty) {
                    sb.append(property.getV()+",");
                }
                sb.deleteCharAt(sb.length()-1);
            }
            item.setIndentStyle(sb.toString());
            view.addView(item);
        }
        TextView tvIndentPrivilege = holder.getTv(R.id.tv_indent_privilege);
        TextView tvPayStatus = holder.getTv(R.id.tv_pay_status);
        tvPayStatus.setOnClickListener(mStatusListener);
        if(orderInfo.getFlag()==1){
            tvPayStatus.setText("立即支付");
            tvPayStatus.setBackgroundResource(R.color.common_color);
        }else if(orderInfo.getFlag()==2){
            tvPayStatus.setText("查看物流");
            tvPayStatus.setBackgroundResource(R.color.line_color);
        }else if(orderInfo.getFlag()==3){
            tvPayStatus.setText("立即评价");
            tvPayStatus.setBackgroundResource(R.color.line_color);
        }
    }
}
